package com.cloudloan.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 *响应返回类
 * @param <T>
 */
@Data
public class R<T>{
    private Integer status_code; //编码：200成功，0和其它数字为失败

    private String message; //错误信息

    private T data; //数据

    private Map map = new HashMap(); //动态数据
    @JsonIgnore
    private ArrayList<String> arr;


    public static <T> R<T> success(T object) {
        R<T> r = new R<T>();
        r.data = object;
        r.status_code = 200;
        return r;
    }

    public R() {
    }

    public static <T> R<T> success(T object, String msg) {
        R<T> r = new R<T>();
        r.data = object;
        r.message=msg;
        r.status_code = 200;
        return r;
    }
    public static <T> R<T> success(T object, String msg,ArrayList<String> arr) {
        R<T> r = new R<T>();
        r.data = object;
        r.message=msg;
        r.status_code = 200;
        r.arr=arr;
        return r;
    }


    public static <T> R<T> error(String msg) {
        R r = new R();
        r.message = msg;
        r.status_code = 0;
        return r;
    }

    public R<T> add(String key, Object value) {
        this.map.put(key, value);
        return this;
    }

}
